<template>
  <div style="padding-bottom:60px;">
    <SearchPart :storageType="storageType" @dblclickPartInfo="setPartInfo" />
    <PartInfo ref="partInfo" :partInfoRow="partInfoRow" type="add" @getEditPartInfo="getEditPartInfo" />
    <el-row class="btn-wrap">
      <el-button size="small" @click="$emit('cancel')">取消</el-button>
      <el-button type="primary" size="small" @click="confirmBtnClick">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { isAuthorized } from '@/utils/index';
import SearchPart from './SearchPart';
import PartInfo from './PartInfo';
// import PartInventory from './PartInventory';
import { getpartStocksList } from '@/api/part/inventoryManager/inventoryManager/index';

export default {
  name: 'wxll_add',
  components: {
    SearchPart,
    PartInfo
    // PartInventory
  },
  props: {
    storageType: {
      type: String
    }
  },
  data() {
    return {
      partInfoRow: {},
      isShowPartInventory: false,
      partStockDetail: {},
      stockFlag: false
    };
  },
  mounted() {
    window.cgrk_add = this;
    console.log('采购入库 新增 mounted');
    this.getSystemAuthority();
  },
  methods: {
    // 确定按钮
    confirmBtnClick() {
      // 收集表单数据 触发 getEditPartInfo 方法
      this.$refs.partInfo.$refs.form.SUBMIT_FORM();
    },
    // 整合后的配件信息
    setPartInfo(partInfo) {
      console.log('整合后的配件信息 浅拷贝', JSON.stringify(partInfo, null, 2));
      this.partInfoRow = partInfo;
      // this.$refs.partInfo.initDataSource(partInfo);
    },
    // 收集 表单数据回调
    getEditPartInfo(partInfo) {
      console.log(partInfo, 'partInfo');
      if (partInfo.PRICE_RATE < 1) {
        if (isAuthorized('1313', '8248')) {
          console.log('表单数据', JSON.stringify(partInfo, null, 2));
          // 给父表加的数据
          this.$emit('confirm', partInfo);
        } else {
          this.$notify({
            title: '警告',
            message: '价格系数小于1，您无权限保存',
            type: 'warning'
          });
        }
      } else {
        this.$emit('confirm', partInfo);
      }
    },
    // 库存明细
    async stockDetail() {
      // if(this.partInfoRow)
      if (JSON.stringify(this.partInfoRow) != '{}') {
        let param = {
          PART_NO: this.partInfoRow.PART_NO,
          STORAGE_CODE1: this.partInfoRow.STORAGE_CODE,
          STORAGE_POSITION_CODE: this.partInfoRow.STORAGE_POSITION_CODE
        };
        let res = await getpartStocksList(param);
        this.partStockDetail = res.data.rows[0];
        this.isShowPartInventory = true;
      } else {
        this.$message({
          showClose: true,
          message: '请先选择一条配件信息',
          type: 'warning'
        });
      }
    },

    // 获取系统权限
    getSystemAuthority() {
      // 库存明细
      let stockDetail = isAuthorized('1313', '13131012');
      console.log(stockDetail, 'stockDetailstockDetailstockDetail');
      if (stockDetail) {
        console.log('13131012----------------------------------');
        this.stockFlag = true;
      }
    }
  }
};
</script>

<style scoped>
.btn-wrap {
  position: absolute;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: 9;
  border-top: 1px solid #e9e9e9;
  padding: 10px 20px;
  background: #fff;
  text-align: right;
}
</style>
